package com.cheng.controller;

import com.cheng.feign.ProviderFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.xml.transform.Result;

@RestController
@RequestMapping("/consumer")
@Slf4j
public class ConsumerController {
    //负载均衡客户端
//    @Autowired
//    private LoadBalancerClient balancerClient;

    @Resource
    private RestTemplate restTemplate;

    @Autowired
    private ProviderFeign providerFeign;

    @GetMapping("/sayHello")
    public String sayHello(){
        log.info("发送RestTemplate请求调用提供者");
        String url = "http://springcloud-provider/provider/hello";
        ResponseEntity<String> resultEntity = restTemplate.getForEntity(url, String.class);
        String body = resultEntity.getBody();
        log.info("提供者返回数据：{}",body);
        return body;

    }

    @GetMapping("/getHello")
    public String getHello(){
        log.info("使用openFeign调用提供者");
        String hello = providerFeign.getHello();
        log.info("提供者返回数据：{}",hello);
        return hello;
    }

    @GetMapping("/getFallback")
    public String getFallback(@RequestParam("flag") String flag){
        String result = "";
        if ("Y".equals(flag)){
            result = providerFeign.getFallback(true);
        }else {
            result = providerFeign.getFallback(false);
        }
        return result;
    }
}
